package com.gs.SynAntiWandering.activity.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.gs.SynAntiWandering.R;
import com.gs.SynAntiWandering.constant.AllConstants;
import com.gs.SynAntiWandering.constant.EventCode;
import com.gs.SynAntiWandering.control.ToolBarSearch;
import com.gs.SynAntiWandering.control.ToolBarTitle;
import com.gs.SynAntiWandering.db.bean.Event;
import com.gs.SynAntiWandering.db.query.BaseQuery;
import com.gs.SynAntiWandering.presenter.BasePresenter;
import com.gs.SynAntiWandering.util.ActivityUtil;
import com.gs.SynAntiWandering.util.LanguageUtils;
import com.gs.SynAntiWandering.util.WindowUtil;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseListActivity<V, T extends BasePresenter<V>, D, Q extends BaseQuery> extends Activity {
    private WeakReference<Activity> weakReference = null;
    protected T mPresenter;
    protected List<D> list;
    protected Q query;
    protected int totalPages;
    protected SmartRefreshLayout smartRefreshLayout;
    protected RecyclerView recyclerView;
    protected ToolBarTitle toolBarTitle;
    protected ToolBarSearch toolBarSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowUtil.setBarBgColor(this, R.color.green);
        list = new ArrayList<>();
        setContentView(this.getLayoutId());
        if (weakReference == null) {
            weakReference = new WeakReference<Activity>(this);
        }
        ActivityUtil.getInstance().addActivity(weakReference.get());
        query = createQuery();
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
        }
        recyclerView = findViewById(R.id.recyclerView);
        toolBarTitle = findViewById(R.id.toolBarTitle);
        toolBarSearch = findViewById(R.id.toolBarSearch);
        smartRefreshLayout = findViewById(R.id.refresh);
        if (smartRefreshLayout != null) {
            //刷新
            smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
                smartRefreshLayout.setTag(AllConstants.REFRESH);
                if (query != null) {
                    list.clear();
                    query.setPageIndex(1);
                    loadData();
                }
            });
            //加载更多
            smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
                smartRefreshLayout.setTag(AllConstants.LOAD_MORE);
                if (query != null) {
                    if (totalPages == 0) {
                        totalPages = 1;
                    }
                    if (totalPages <= query.getPageIndex())
                        query.setPageIndex(++totalPages);
                    else
                        query.setPageIndex(query.getPageIndex() + 1);
                    loadData();
                }
            });
        }

        if (toolBarTitle != null) {
            toolBarTitle.setOnClickListener(v -> {
                int id = v.getId();
                if (id == R.id.item_back) {
                    finish();
                } else if (id == R.id.item_per) {
                    add();
                }
            });
        }
        if (toolBarSearch != null) {
            toolBarSearch.setOnClickListener(v -> {
                if (v.getId() == R.id.item_per) {
                    search();
                }
            });
        }
        initView();
        search();
    }

    public void refreshSmartRefreshLayout() {
        if (smartRefreshLayout.getTag() == null)
            return;
        if (smartRefreshLayout.getTag().equals(AllConstants.REFRESH)) {
            smartRefreshLayout.finishRefresh();
        } else {
            smartRefreshLayout.finishLoadMore();
        }
    }

    protected <I extends View> I fd(int id) {
        return findViewById(id);
    }

    public abstract int getLayoutId();

    public abstract void initView();

    public abstract void loadData();

    public abstract void add();

    public abstract void search();

    public abstract T createPresenter();

    public abstract Q createQuery();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        ActivityUtil.getInstance().removeActivity(weakReference.get());
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(LanguageUtils.getInstance().change(LanguageUtils.getInstance().getCurLocale()));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event) {
        if (event.getWhat() == EventCode.EVENT_LANGUAGE) {
            recreate();
        } else if (event.getWhat() == EventCode.EVENT_SITE) {
            recreate();
        }
    }
}